<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class home extends CI_Controller {
	var $title = "Jogja Nuws";
	var $cust_validation_msg = array(
		'alpha_dash'	=> 'Input harus berupa karakter, angka dan dash',
		'required'		=> '%s harus di isi',
		'matches'		=> '%s tidak cocok',
		'valid_email'	=> 'Isikan email yang valid'
		);

	public function __construct(){
		parent::__construct();
		$this->load->model(array('user_model', 'post_model', 'kategori_model', 'page_model'));
	}

	public function index($page_id=0){
		$data['title'] = $this->title;
		$data['desc'] = "Jogja Nuwun Sewu";
		$data['category'] = $this->kategori_model->get_all();
		$data['recommend_post'] = $this->post_model->get_all_rand();

		$setting = $this->settings();
		$page_query = $this->post_model->home_post();		
		$paginate_data = array(
			'page_id'		=> $page_id,
			'query'			=> $page_query,
			'base_url'		=> 'index',
			'uri_segment'	=> 3,
			'per_page'		=> $setting['home_pagination'],
			'num_links'		=> 3
			);
		$data['post'] = $this->post_paginator($paginate_data);

		$this->load->view('home', $data);
	}

	private function post_paginator($data){
		$this->load->library('pagination');
		$jml = $data['query'];
		//pengaturan pagination
		$config['base_url'] = site_url()."/home/".$data['base_url'];
		$config['uri_segment'] = $data['uri_segment'];
		$config['total_rows'] = count($jml);
		$config['per_page'] = $data['per_page'];
		$config['num_links'] = $data['num_links'];

		/*designing pagination*/
		$config['full_tag_open'] = "<span>";
		$config['full_tag_close'] = "</span>";
		$config['prev_link'] = '<i class="fa fa-backward"></i>';
		$config['prev_tag_open'] = "<span class='page_nav'>";
		$config['prev_tag_close'] = "</span>";
		$config['num_tag_open'] = "<span>";
		$config['num_tag_close'] = "</span>";
		$config['next_link'] = '<i class="fa fa-forward"></i>';
		$config['next_tag_open'] = "<span class='page_nav'>";
		$config['next_tag_close'] = "</span>";
		$config['cur_tag_open'] = "<span class='current'>";
		$config['cur_tag_close'] = "</span>";

		$this->pagination->initialize($config);
        //buat pagination
		$data['halaman'] = $this->pagination->create_links();
		$data['query'] = $this->post_model->home_post_paged($config['per_page'], $data['page_id']);
		$data['total'] = $config['total_rows'];

		return $data;
	}

	public function like($id){
		if($id != ""){
			$input = $this->input->post();
			if($input['btn_like'] != ""){
				$this->post_model->like($id, get_cookie('c_usr_id'));
				redirect(site_url());
			}else{
				redirect(site_url());		
			}
		}else{
			redirect(site_url());
		}
	}

	public function dislike($id){
		if($id != ""){
			$input = $this->input->post();
			if($input['btn_dislike'] != ""){
				$this->post_model->dislike($id, get_cookie('c_usr_id'));
				redirect(site_url());
			}else{
				redirect(site_url());		
			}
		}else{
			redirect(site_url());
		}
	}
	public function register(){
		$data['title'] = $this->title;
		$data['desc'] = "User Register";
		
		$input = $this->input->post();
		if(!empty($input['btn_register'])){
			$this->validate_regist();
			if($this->form_validation->run() == true){
				$do_register = $this->user_model->store($input);
				if($do_register == FALSE){
					$this->session->set_flashdata('notif_status', '0');
					$this->session->set_flashdata('notif', 'Register gagal');
				}else{
					$conf_id = $do_register['jonu_user_id'];
					$conf_usr = $do_register['jonu_user_username'];
					$conf_key = $do_register['jonu_user_conf_key'];
					$conf_uri = site_url("home/verify/".$conf_id."/".$conf_usr."/".$conf_key);
					$conf_link = "<a href='$conf_uri' target='_blank'>Verify</a>";
					print_r($do_register);
					$this->session->set_flashdata('notif_status', '1');
					$this->session->set_flashdata('notif', "<a href='$conf_uri' target='_blank'>Verify</a>");
				}
				redirect(site_url("home/register"));
			}else{
				$this->load->view('kholid_views/register', $data);
			}
		}else{
			$this->load->view('kholid_views/register', $data);
		}
	}

	private function validate_regist(){	
		$rules = array(
			array(
				'field'	=> 'usr_username',
				'label'	=> 'Username',
				'rules'	=> 'required|alpha_dash'
				),
			array(
				'field'	=> 'usr_fullname',
				'label'	=> 'Nama Lengkap',
				'rules'	=> 'required'
				),
			array(
				'field'	=> 'usr_pass',
				'label'	=> 'Password',
				'rules'	=> 'required'
				),
			array(
				'field'	=> 'usr_repass',
				'label'	=> 'Password',
				'rules'	=> 'required|matches[usr_pass]'
				),
			array(
				'field'	=> 'usr_email',
				'label'	=> 'Email',
				'rules'	=> 'required|valid_email'
				),
			array(
				'field'	=> 'usr_jk',
				'label'	=> 'Jenis Kelamin',
				'rules'	=> 'required'
				)
			);
		$this->load->library('form_validation');
		$this->form_validation->set_error_delimiters('','');	
		$this->form_validation->set_message($this->cust_validation_msg);
		$this->form_validation->set_rules($rules);		
	}

	public function verify($id, $username, $sec_key){
		$data_verify1 = $this->user_model->verify($id, $username, $sec_key);
		if(count($data_verify1) == 1){
			$confirm_id = $data_verify1[0]['jonu_user_id'];
			$confirm_username = $data_verify1[0]['jonu_user_username'];

			$do_verify = $this->user_model->do_verify($confirm_id, $confirm_username);
			if($do_verify){
				$usr = $this->uri->segment(4);
				echo "Email dan status user $usr sudah dikonfimasi. Terima Kasih.";
			}else{
				echo "Konfirmasi email dan status user gagal";
			}
		}else{
			echo "ndak";
		}
	}

	public function category($cat_slug){
		$data['title'] = $this->title;
		$data['desc'] = "Category $cat_slug";
		$data['category'] = $this->kategori_model->get_all();
		$data['recommend_post'] = $this->post_model->get_all_rand();
		$data['post_per_cat'] = $this->post_model->get_by_cat($cat_slug);

		$this->load->view('post_per_cat', $data);
	}

	public function page($permalink){
		if(empty($permalink) || empty($title)){
			$data['title'] = $this->title;
			$data['category'] = $this->kategori_model->get_all();
			$data['recommend_post'] = $this->post_model->get_all_rand();
			$data['page'] = $this->page_model->get_by_permalink($permalink);

			$this->load->view('page', $data);
		}else{
			redirect(site_url());
		}
	}

	private function settings(){
		$this->load->model('settings_model');

		$q_per_page_setting = $this->settings_model->get_all();
		foreach($q_per_page_setting as $q_per_page_setting_rec){
			if($q_per_page_setting_rec['jonu_setting_id'] == "home_pagination"){
				$setting['home_pagination'] = $q_per_page_setting_rec['jonu_setting_value'];
			}
		}

		return $setting;
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */